package io.finer.erp.jeecg.bas.service.impl;

import io.finer.erp.jeecg.bas.entity.ZzClassOrder;
import io.finer.erp.jeecg.bas.entity.ZzClassOrderRest;
import io.finer.erp.jeecg.bas.mapper.ZzClassOrderRestMapper;
import io.finer.erp.jeecg.bas.mapper.ZzClassOrderMapper;
import io.finer.erp.jeecg.bas.service.IZzClassOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 班次
 * @Author: jeecg-boot
 * @Date:   2020-08-25
 * @Version: V1.0
 */
@Service
public class ZzClassOrderServiceImpl extends ServiceImpl<ZzClassOrderMapper, ZzClassOrder> implements IZzClassOrderService {

	@Autowired
	private ZzClassOrderMapper zzClassOrderMapper;
	@Autowired
	private ZzClassOrderRestMapper zzClassOrderRestMapper;
	
	@Override
	@Transactional
	public void saveMain(ZzClassOrder zzClassOrder, List<ZzClassOrderRest> zzClassOrderRestList) {
		zzClassOrderMapper.insert(zzClassOrder);
		if(zzClassOrderRestList!=null && zzClassOrderRestList.size()>0) {
			for(ZzClassOrderRest entity:zzClassOrderRestList) {
				//外键设置
				entity.setMainId(zzClassOrder.getId());
				zzClassOrderRestMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ZzClassOrder zzClassOrder,List<ZzClassOrderRest> zzClassOrderRestList) {
		zzClassOrderMapper.updateById(zzClassOrder);
		
		//1.先删除子表数据
		zzClassOrderRestMapper.deleteByMainId(zzClassOrder.getId());
		
		//2.子表数据重新插入
		if(zzClassOrderRestList!=null && zzClassOrderRestList.size()>0) {
			for(ZzClassOrderRest entity:zzClassOrderRestList) {
				//外键设置
				entity.setMainId(zzClassOrder.getId());
				zzClassOrderRestMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		zzClassOrderRestMapper.deleteByMainId(id);
		zzClassOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			zzClassOrderRestMapper.deleteByMainId(id.toString());
			zzClassOrderMapper.deleteById(id);
		}
	}
	
}
